package com.suke.czx.modules.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suke.czx.common.utils.CodeConstant;
import com.suke.czx.common.utils.R;
import com.suke.czx.modules.contact.service.ContactService;
import com.suke.czx.modules.customer.entity.Customer;
import com.suke.czx.modules.customer.mapper.CustomerMapper;
import com.suke.czx.modules.customer.service.CustomerService;
import com.suke.czx.modules.project.mapper.ProjectMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 应用模块名称: 客户
 * 代码描述: 客户业务层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/08/31 10:17:04
 */
@Service
@AllArgsConstructor
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    private final CustomerMapper customerMapper;

    private final ContactService contactService;

    private final ProjectMapper projectMapper;

    /**
     * 删除客户
     * @param customerId 客户编码
     * @return VO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateDelCustomer(Long customerId, Long userId) {
        // 有对应项目的客户不能删除
        if (projectMapper.countCustomerProject(customerId) > 0) {
            return R.error("有对应项目的客户不能删除");
        }
        // 删除客户
        customerMapper.updateDelCustomer(customerId, userId);
        // 删除客户对应所有联系人
        contactService.updateDelContactByRelation(CodeConstant.CONTACT_TYPE_CUSTOMER, customerId, userId);
        return R.ok();
    }

}
